package com.sls.web.manager.action.ekhui;

import java.math.BigDecimal;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.game.client.AccountClient;
import com.sls.web.manager.game.client.CommissionPayClient;
import com.sls.web.manager.game.pojo.ekhui.Account;
import com.sls.web.manager.game.pojo.ekhui.CommissionPay;
import com.sls.web.manager.game.pojo.ekhui.Trader;
import com.sls.web.manager.game.querybean.AccountQuery;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;

public class AccountAction extends SuperAction{
	private Logger log = LoggerFactory.getLogger(AccountAction.class) ;
	private List<Account> accountList  ;
	private Pagination<Account> page;
	private Account account ;
	private Account payAccount ;
	private CommissionPay commissionPay ;
	private AccountQuery accountQuery ;
	protected String startTime ;
	protected String endTime ;
	protected int recordCount ;
	public String list(){
		UserInfoVO userInfoVO = getCurrentOperator() ;
		AccountClient client = AccountClient.getInstance() ;
		if(account == null){
			account = new Account() ;
		}
		if (accountQuery == null) {
			accountQuery = new AccountQuery();
			accountQuery.setPageSize(Constants.INIT_PAGESIZE);
			accountQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		recordCount = client.getAccountCount(userInfoVO, account) ;
		if(page == null){
			page = new Pagination<Account>(accountQuery.getCurrentPage() , recordCount , accountQuery.getPageSize()) ;
		}
		accountList = client.getAccountList(userInfoVO, account , accountQuery.getCurrentPage() , accountQuery.getPageSize()) ;
		page.setList(accountList) ;
		page.setRowCount(recordCount) ;
		return "list" ;
	}
	public String commission(){
		
		return "commission" ;
	}
	public String commissionComplete(){
		UserInfoVO userInfoVO = getCurrentOperator() ;
		commissionPay.setMoney(commissionPay.getMoney().multiply(new BigDecimal("1000"))) ;
		CommissionPayClient client = CommissionPayClient.getInstance() ;
		AccountClient accountClient = AccountClient.getInstance();
		Trader payTrader = new Trader() ;
		payTrader.setTraderId(Constants.PAYEE_ACCOUNT) ;
		Account payAccount = accountClient.getAccountByTrader(userInfoVO, payTrader) ;
		if(payAccount.getAvailableMoney().compareTo(commissionPay.getMoney()) < 0){
			message = "余额不足" ;
		}else{
			boolean res = client.saveCommissionPay(payAccount ,account, commissionPay, userInfoVO) ;
			if(res){
				message = "返佣成功" ;
			}else{
				message = "返佣失败" ;
			}
		}
		
		return "ajax" ;
	}
	public String enableOrDis(){
		UserInfoVO userInfoVO = getCurrentOperator() ;
		AccountClient accountClient = AccountClient.getInstance();
		message = String.valueOf(accountClient.updateAccountState(userInfoVO, account)) ;
		return "ajax" ;
	}
	/**
	 * 账户在账务日期的期初和期末余额
	 * @return
	 */
	public String accountBeginAndEnd(){
		return "accountBeginAndEnd" ;
	}
	public List<Account> getAccountList() {
		return accountList;
	}
	public void setAccountList(List<Account> accountList) {
		this.accountList = accountList;
	}
	public Pagination<Account> getPage() {
		return page;
	}
	public void setPage(Pagination<Account> page) {
		this.page = page;
	}
	public Account getAccount() {
		return account;
	}
	public void setAccount(Account account) {
		this.account = account;
	}
	public AccountQuery getAccountQuery() {
		return accountQuery;
	}
	public void setAccountQuery(AccountQuery accountQuery) {
		this.accountQuery = accountQuery;
	}
	public String getStartTime() {
		return startTime;
	}
	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}
	public String getEndTime() {
		return endTime;
	}
	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	public int getRecordCount() {
		return recordCount;
	}
	public void setRecordCount(int recordCount) {
		this.recordCount = recordCount;
	}
	public CommissionPay getCommissionPay() {
		return commissionPay;
	}
	public void setCommissionPay(CommissionPay commissionPay) {
		this.commissionPay = commissionPay;
	}
	public Account getPayAccount() {
		return payAccount;
	}
	public void setPayAccount(Account payAccount) {
		this.payAccount = payAccount;
	}
}
